package com.zusmart.base.network.handler.support;

import java.io.IOException;

import com.zusmart.base.buffer.Buffer;
import com.zusmart.base.network.ChannelContext;
import com.zusmart.base.network.handler.ChannelContextHandler;
import com.zusmart.base.network.handler.ChannelContextHandlerContext;
import com.zusmart.base.network.message.Message;

public class DefaultChannelContextHandler implements ChannelContextHandler {

	@Override
	public void onRegister(ChannelContext channelContext, ChannelContextHandlerContext handlerContext) {
		ChannelContextHandlerContext context = handlerContext.getNext();
		if (null != context) {
			context.getHandler().onRegister(channelContext, context);
		}
	}

	@Override
	public void unRegister(ChannelContext channelContext, ChannelContextHandlerContext handlerContext) {
		ChannelContextHandlerContext context = handlerContext.getNext();
		if (null != context) {
			context.getHandler().unRegister(channelContext, context);
		}
	}

	@Override
	public void onMessage(ChannelContext channelContext, ChannelContextHandlerContext handlerContext, Message message) {
		ChannelContextHandlerContext context = handlerContext.getNext();
		if (null != context) {
			context.getHandler().onMessage(channelContext, context, message);
		}
	}

	@Override
	public void onException(ChannelContext channelContext, ChannelContextHandlerContext handlerContext, Throwable cause) {
		ChannelContextHandlerContext context = handlerContext.getNext();
		if (null != context) {
			context.getHandler().onException(channelContext, context, cause);
		}
	}

	@Override
	public void onTimeout(ChannelContext channelContext, ChannelContextHandlerContext handlerContext) {
		ChannelContextHandlerContext context = handlerContext.getNext();
		if (null != context) {
			context.getHandler().onTimeout(channelContext, context);
		}
	}

	@Override
	public void onReader(ChannelContext channelContext, ChannelContextHandlerContext handlerContext, Buffer buffer) throws IOException {
		ChannelContextHandlerContext context = handlerContext.getNext();
		if (null != context) {
			context.getHandler().onReader(channelContext, context, buffer);
		}
	}

}